package com.jeff.controller;

import com.jeff.common.BaseInfo;
import com.jeff.common.mConst;
import com.jeff.po.Form;
import com.jeff.service.FormService;
import com.jeff.util.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("form")
public class FormController {

    @Autowired
    public FormService formService;

    @RequestMapping("add")
    @ResponseBody
    public ResultMap add(HttpServletRequest request, Form form) {
        ResultMap rm = new ResultMap();
        try {
            form.setState(mConst.STATE_T);
            formService.add(form);
            rm.success().info("增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("增加失败");
        }
        return rm;
    }

    @RequestMapping("list")
    @ResponseBody
    public ResultMap list() {
        ResultMap rm = new ResultMap();
        try {
            Form form = new Form();
            form.setId(null);
            form.setState(mConst.STATE_T);
            List<Form> list = formService.list(form);
            rm.success().data(list);
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }


    @RequestMapping("page")
    @ResponseBody
    public ResultMap page(Form form) {
        ResultMap rm = new ResultMap();
        try {
            form.setId(null);
            form.setState(mConst.STATE_T);
            rm.success().page(formService.findByPage(form));
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }


    @RequestMapping("update")
    @ResponseBody
    public ResultMap update(Form form) {
        ResultMap rm = new ResultMap();
        try {
            form.setLoginName(null);
    /*		if(!StringUtil.isEmpty(form.getCard())){
				form.setLoginName(form.getCard());
				Form check = new Form();
				check.setState(mConst.STATE_T);
				check.setId(null);
				check.setLoginName(form.getLoginName());
				List<Form> checkList = formService.list(check);
				if(checkList!=null&&checkList.size()!=0){
					if(checkList.size()==1){
						//是本人，本人重复传了相同的card
						if(!checkList.get(0).getId().equals(form.getId())){
							return rm.fail().info("修改失败,有重名账号");
						}

					}else{
						return rm.fail().info("修改失败,有重名账号");
					}
				}
			}*/

            formService.update(form);
            rm.success().info("修改成功");
        } catch (Exception e) {
            rm.fail().info("修改失败");
        }
        return rm;
    }
	
	

/*	@RequestMapping("check")
	@ResponseBody
	public Boolean check(Form form) {
		try {
			Form us = formService.check(form);
			if (us != null) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		return true;

	}*/

    @RequestMapping("remove")
    @ResponseBody
    public ResultMap remove(Form form) {
        ResultMap rm = new ResultMap();
        try {
            form.setState(mConst.STATE_F);
            formService.update(form);
            rm.success().info("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("删除失败");
        }
        return rm;
    }


}
